package role

import (
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type Role struct {
	Id          primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
	Code        string             `bson:"code" json:"code,omitempty" ` //通过CODE关联
	Description string             `bson:"description,omitempty" json:"description,omitempty"  `
	Permissions []string           `bson:"permissions" json:"permissions,omitempty"` //Permission.code
}

func (r Role) CollectionName() string {
	return "role"
}

func (r Role) GetIndex() []mongo.IndexModel {
	return []mongo.IndexModel{
		{Keys: bson.D{{"code", 1}}, Options: options.Index().SetUnique(true)},
	}
}

// Permission 前端通过用户拥有的permission 来判断页面展示
type Permission struct {
	Id             primitive.ObjectID `bson:"_id,omitempty"`
	Code           string             `bson:"code"  binding:"required"` //通过CODE关联
	Description    string             `bson:"description,omitempty"  binding:"required"`
	Routers        []string           `bson:"routers,omitempty"` //router.id
	Parent         string             `bson:"parent,omitempty"`
	SubPermissions []*Permission      `bson:"-"`
}

func (p Permission) CollectionName() string {
	return "permission"
}

func (p Permission) GetIndex() []mongo.IndexModel {
	return []mongo.IndexModel{
		{Keys: bson.D{{"code", 1}}, Options: options.Index().SetUnique(true)},
		{Keys: bson.D{{"parent", 1}}},
	}
}
